package com.cherry.common.util;


import java.util.Map;

/**
 * 占位符解析工具类
 *
 * @author sinbad.cheng
 * @since 2022-07-21
 */
public final class PlaceholderUtil {

    private PlaceholderUtil() {
    }

    /**
     * 解析数据
     *
     * @param text 文本
     * @param data 数据
     * @return 解析后的文本
     */
    public static String parse(String text, Object data) {
        return parse(text, "${", "}", data);
    }

    /**
     * 解析数据
     *
     * @param text 文本
     * @param data 数据
     * @return 解析后的文本
     */
    public static String parse(String text, Map<String, Object> data) {
        return parse(text, "${", "}", data);
    }

    /**
     * 解析数据
     *
     * @param text       文本
     * @param openToken  占位符开始
     * @param closeToken 占位符结束
     * @param data       数据
     * @return 解析后的文本
     */
    public static String parse(String text, String openToken, String closeToken, Object data) {
        if (data == null) {
            return text;
        }

        Map<String, Object> hashMap = BeanUtil.toMap(data);
        return parse(text, openToken, closeToken, hashMap);
    }

    /**
     * 解析文本
     *
     * @param text       文本
     * @param openToken  占位符开始
     * @param closeToken 占位符结束
     * @param data       填充数据
     * @return 解析后的文本
     */
    public static String parse(String text, String openToken, String closeToken, Map<String, Object> data) {
        if (text == null || text.isEmpty()) {
            return "";
        }
        if (data == null || data.isEmpty()) {
            return text;
        }

        // search open token
        int start = text.indexOf(openToken);
        if (start == -1) {
            return text;
        }
        char[] src = text.toCharArray();
        int offset = 0;
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;
        do {
            if (start > 0 && src[start - 1] == '\\') {
                // this open token is escaped. remove the backslash and continue.
                builder.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                // found open token. let's search close token.
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        // this close token is escaped. remove the backslash and continue.
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        break;
                    }
                }
                if (end == -1) {
                    // close token was not found.
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    Object value = data.get(expression.toString());
                    builder.append(value == null ? "" : value.toString());
                    offset = end + closeToken.length();
                }
            }
            start = text.indexOf(openToken, offset);
        } while (start > -1);
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }

}